package com.example.repeatsubmit.controller;

import com.alibaba.fastjson.JSON;
import com.example.repeatsubmit.annotation.CloseRepeatSubmit;
import com.example.repeatsubmit.entity.Param;
import com.example.repeatsubmit.enums.RepeatSubmitPolicyEnum;
import com.example.repeatsubmit.utils.ApplicationContextUtil;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author caoqianhuan
 * @date 2022/8/22
 * @Description:
 */
@RestController
@Validated
public class Test {
    @Value("${oss.logoutUrl}")
    private String logoutUrl;


    @GetMapping("/a")
    @CloseRepeatSubmit(policy = RepeatSubmitPolicyEnum.TIME_INNER_REFUSE, interval = 2000)
    public void test() {
        //System.out.println(ApplicationContextUtil.getBean("stringRedisTemplate"));
        System.out.println("/a");
    }

    @GetMapping("/b")
    @CloseRepeatSubmit(policy = RepeatSubmitPolicyEnum.CACEH_LOCK, interval = 2000)
    public void test1() {

    }
    @GetMapping("/c")
    @CloseRepeatSubmit(policy = RepeatSubmitPolicyEnum.REDIS_LOCK, interval = 10000)
    public void test2() {
        System.out.println("test success");
        //System.out.println(ApplicationContextUtil.getBean("stringRedisTemplate"));
    }

    @RequestMapping("/d")
    public void test3(HttpServletRequest request) {
        //System.out.println(ApplicationContextUtil.getContext());

        Map<String, Object> map = new HashMap<>();
        map.put("url", request.getRequestURL().toString());
        System.out.println(JSON.toJSONString(map));
    }

    @RequestMapping("/e")
    public void testValid(@Validated  Param a) {
        System.out.println(a);
    }
    @RequestMapping("/q")
    public void testMyContrant(Param b) {
        System.out.println(b);
    }
    @PostMapping("/f")
    public void testRequestBody(@RequestBody Param param) {
        System.out.println(param);
    }

    @PostMapping("/j/{a}")
    public void testForm(@PathVariable("a") String a) {
        System.out.println(a);
    }

    @GetMapping("/k")
    public void test4(@RequestParam("b") String a) {
        System.out.println(a);
    }

    @RequestMapping("/l")
    //@Validated
    public void testSpring(@NotNull(message = "不能为空") String a) {
        System.out.println(a);
    }

    @RequestMapping("/m/{a}/{b}")
    public void testRestFul(@PathVariable("a") String aa, @PathVariable("b") String bb) {
        System.out.println(aa);
        System.out.println(bb);
    }
    @RequestMapping("/n")
    public void testInterceptor(String n) {
        System.out.println(n);
    }
    @RequestMapping("/logout")
    public void logout(String ticket, HttpServletResponse response) throws IOException {

        response.sendRedirect("redirect:" + logoutUrl + "?ticket=" + ticket );
    }

    /**
     * 分布式远程调用 这里使用http请求模拟一下
     */
    @RequestMapping("/realLogout")
    public Boolean realLogout(HttpSession session) {
        session.removeAttribute("isLogin");
        if ( session.getAttribute("isLogin") != null && (Boolean) session.getAttribute("isLogin") ) {
            return false;
        }
        return true;
    }
}
